<!DOCTYPE html>
<html>
<head>
<title>Coaxial Cable Diagram</title>
</head>
<body>

<canvas id="coaxCanvas" width="750" height="500"></canvas>

<script>
const canvas = document.getElementById('coaxCanvas');
const ctx = canvas.getContext('2d');

// Style settings
ctx.strokeStyle = 'black';
ctx.fillStyle = 'black';
ctx.lineWidth = 2;
ctx.font = '24px sans-serif';

const centerY = 250;

// --- Draw the cable components from inside out ---

// 1. Copper Wire
ctx.beginPath();
ctx.lineWidth = 3;
ctx.moveTo(100, centerY);
ctx.lineTo(220, centerY);
ctx.stroke();
ctx.lineWidth = 2; // Reset line width

// 2. Insulation
const insulation_x = 220;
const insulation_radius_y = 30;
const insulation_radius_x = 8;
ctx.beginPath();
ctx.ellipse(insulation_x, centerY, insulation_radius_x, insulation_radius_y, Math.PI / 2, 0, 2 * Math.PI);
ctx.stroke();

// 3. Copper Braid
const braid_start_x = 220;
const braid_end_x = 340;
const braid_radius_y = 40;
const braid_radius_x = 10;

// Braid pattern
ctx.save();
// Clip to the rectangular area of the braid's visible surface
ctx.beginPath();
ctx.rect(braid_start_x, centerY - braid_radius_y, braid_end_x - braid_start_x, 2 * braid_radius_y);
ctx.clip();

ctx.lineWidth = 1.5;
// Draw criss-cross lines to represent the braid
for (let x = braid_start_x - 100; x < braid_end_x + 100; x += 20) {
    // Lines leaning right
    ctx.beginPath();
    ctx.moveTo(x, centerY - braid_radius_y);
    ctx.lineTo(x + 45, centerY + braid_radius_y);
    ctx.stroke();
    // Lines leaning left
    ctx.beginPath();
    ctx.moveTo(x + 45, centerY - braid_radius_y);
    ctx.lineTo(x, centerY + braid_radius_y);
    ctx.stroke();
}
ctx.restore();
ctx.lineWidth = 2; // Reset line width

// Draw the outline of the braid section
ctx.beginPath();
ctx.moveTo(braid_start_x, centerY - insulation_radius_y);
ctx.lineTo(braid_end_x, centerY - braid_radius_y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(braid_start_x, centerY + insulation_radius_y);
ctx.lineTo(braid_end_x, centerY + braid_radius_y);
ctx.stroke();

// 4. Plastic Covering
const plastic_start_x = 340;
const plastic_end_x = 650;
const plastic_radius_y = 55;
const plastic_radius_x = 12;

// End cap of the braid/start of the plastic
ctx.beginPath();
ctx.ellipse(plastic_start_x, centerY, braid_radius_x, braid_radius_y, Math.PI / 2, 0, 2 * Math.PI);
ctx.stroke();
// End cap for the plastic layer
ctx.beginPath();
ctx.ellipse(plastic_start_x, centerY, plastic_radius_x, plastic_radius_y, Math.PI / 2, 0, 2 * Math.PI);
ctx.stroke();

// Main body of the plastic covering
ctx.beginPath();
ctx.moveTo(plastic_start_x, centerY - plastic_radius_y);
ctx.quadraticCurveTo(500, centerY - plastic_radius_y - 10, plastic_end_x, centerY - plastic_radius_y + 5);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(plastic_start_x, centerY + plastic_radius_y);
ctx.quadraticCurveTo(500, centerY + plastic_radius_y + 10, plastic_end_x, centerY + plastic_radius_y - 5);
ctx.stroke();

// Jagged end of the cable
const jagged_x = plastic_end_x;
const jagged_y_top = centerY - plastic_radius_y + 5;
const jagged_y_bottom = centerY + plastic_radius_y - 5;
const jag_depth = 20;
ctx.beginPath();
ctx.moveTo(jagged_x, jagged_y_top);
ctx.lineTo(jagged_x - jag_depth, jagged_y_top + 25);
ctx.lineTo(jagged_x, jagged_y_top + 50);
ctx.lineTo(jagged_x - jag_depth, jagged_y_top + 75);
ctx.lineTo(jagged_x, jagged_y_bottom);
ctx.stroke();


// --- Labels and Leader Lines ---
ctx.lineWidth = 1;
ctx.textAlign = 'left';

// Label: copper wire
ctx.fillText('copper', 100, 350);
ctx.fillText('wire', 100, 375);
ctx.beginPath();
ctx.moveTo(160, 345);
ctx.lineTo(190, 260);
ctx.stroke();

// Label: insulation
ctx.fillText('insulation', 120, 160);
ctx.beginPath();
ctx.moveTo(210, 165);
ctx.lineTo(215, 225);
ctx.stroke();

// Label: copper braid
ctx.fillText('copper braid', 280, 100);
ctx.beginPath();
ctx.moveTo(350, 115);
ctx.lineTo(320, 210);
ctx.stroke();

// Label: plastic covering
ctx.fillText('plastic covering', 520, 400);
ctx.beginPath();
ctx.moveTo(590, 395);
ctx.lineTo(550, 310);
ctx.stroke();

// Figure Caption
ctx.textAlign = 'center';
ctx.fillText('Fig. 5.1', 375, 480);

</script>

</body>
</html>